﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TI_BAL;


namespace TInventory
{
    public partial class SupplierListingForm : Form
    {
        TIEntities dbContext;
        
        public SupplierListingForm()
        {
            InitializeComponent();
            dbContext = new TIEntities();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonAddSupplier_Click(object sender, EventArgs e)
        {
            AddEditSupplier form = new AddEditSupplier();


            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void RefreshGrid()
        {
            try
            {
                //var listOfSuppliers = (from sup in dbContext.Suppliers select sup).ToList<Supplier>();
                var listOfSuppliers = (from sup in dbContext.Suppliers select new { sup.ID, sup.CompanyName, sup.ContactPersonName, sup.SupplierPhoneNumber, sup.SupplierAddress, sup.City, sup.SupplierFax, sup.SupplierEmailAddress, sup.OtherInformation }).ToList();
                dataGridViewSupplier.DataSource = listOfSuppliers;

                dataGridViewSupplier.Columns[0].Visible = false;

                //dataGridViewSupplier.Columns["ID"].HeaderCell.Value = "ddd";
                dataGridViewSupplier.Columns["CompanyName"].HeaderCell.Value = "Company Name";
                dataGridViewSupplier.Columns["ContactPersonName"].HeaderCell.Value = "Contact Person Name";
                dataGridViewSupplier.Columns["SupplierPhoneNumber"].HeaderCell.Value = "Phone #";
                dataGridViewSupplier.Columns["SupplierFax"].HeaderCell.Value = "Supplier Fax";
                dataGridViewSupplier.Columns["SupplierEmailAddress"].HeaderCell.Value = "Email Address";
                dataGridViewSupplier.Columns["SupplierAddress"].HeaderCell.Value = "Supplier Address";
                dataGridViewSupplier.Columns["OtherInformation"].HeaderCell.Value = "Other Information";
                dataGridViewSupplier.Columns["City"].HeaderCell.Value = "City";



            }
            catch (Exception e1)
            {


            }
        }

        private void SupplierListingForm_Load(object sender, EventArgs e)
        {
            RefreshGrid();
            
        }

        private void buttonEditSelected_Click(object sender, EventArgs e)
        {
            AddEditSupplier form = new AddEditSupplier();

            form.selectedRecord = dataGridViewSupplier.SelectedRows[0];
            form.MdiParent = this.MdiParent;

            form.Show();
            form.WindowState = FormWindowState.Minimized;
            form.WindowState = FormWindowState.Maximized;
        }

        private void buttonDeleteSelected_Click(object sender, EventArgs e)
        {
            int id = int.Parse(dataGridViewSupplier.SelectedRows[0].Cells["ID"].Value.ToString());
            Supplier sup = (from s in dbContext.Suppliers where s.ID == id select s).FirstOrDefault();
            dbContext.DeleteObject(sup);
            dbContext.SaveChanges();
            RefreshGrid();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            

        }

        private void buttonDeleteSelected_ControlRemoved(object sender, ControlEventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RefreshGrid();
        }

      
    }
}
